<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="120px">

      <el-form-item label="业主姓名">
        <el-input v-model="form.name" />
      </el-form-item>

      <el-form-item label="手机号">
        <el-input v-model="form.phone" />
      </el-form-item>

      <el-form-item label="出生日期">
        <el-date-picker
          v-model="form.birthdate"
          type="date"
          placeholder="选择出生日期"
          style="width: 100%;"
          @change="handleDateChange"
        />
      </el-form-item>

      <el-form-item label="所属小区">
        <el-select v-model="form.communityId" placeholder="请选择" @change="fetchBuildingList">
          <el-option
            v-for="item in communityList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="所属楼栋">
        <el-select v-model="form.buildingId" placeholder="请选择">
          <el-option
            v-for="item in buildingList"
            :key="item.id"
            :label="item.number"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">确认提交</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getCommunityList } from '@/api/community'
import { validSubmitform } from '@/utils/validate'
import { addRow } from '@/api/owner'
import { getBuildingList } from '@/api/building'
import { parseTime } from '@/utils/index'

export default {
  data() {
    return {
      form: {
        name: '',
        phone: '',
        birthdate: '',
        communityId: '',
        buildingId: ''
      },
      communityList: [],
      buildingList: []
    }
  },
  mounted() {
    this.fetchCommunityList()
  },
  methods: {
    fetchBuildingList() {
      getBuildingList({ communityId: this.form.communityId }).then(response => {
        this.buildingList = response.data
        console.log(response.data)
      })
    },
    fetchCommunityList() {
      getCommunityList().then(response => {
        this.communityList = response.data
      })
    },
    handleDateChange() {
      this.form.birthdate = parseTime(this.form.birthdate, '{y}-{m}-{d}')
    },
    onReset() {
      this.form = {
        name: '',
        phone: '',
        birthdate: '',
        communityId: '',
        buildingId: ''
      }
      this.$message({
        message: '已重置',
        type: 'warning'
      })
    },
    onSubmit() {
      console.log('表单数据:', this.form)
      if (validSubmitform(this.form)) {
        addRow(this.form)
          .then(response => {
            this.$message({
              message: '添加成功',
              type: 'success'
            })
          })
      } else {
        this.$message({
          message: '请填写完整信息',
          type: 'warning'
        })
      }
    }

  }
}
</script>

<style scoped>
.line{
  text-align: center;
}
</style>

